package com.starsea.book.ui.newpage.adapter

import android.content.Context
import android.text.TextUtils
import android.view.ViewGroup
import com.starsea.book.base.adapter.ItemViewHolder
import com.starsea.book.base.adapter.RecyclerAdapter
import com.starsea.book.databinding.ItemCoinExchangeBinding
import com.starsea.book.ui.newpage.entity.WalletWithDrawEntity
import com.starsea.book.ui.newpage.utils.finialDouble
import com.starsea.book.ui.widget.recycler.ItemTouchCallback

class CoinExchangeAdapter(
    context: Context,
    var subRatio: String,
    var onChanged: ((value: WalletWithDrawEntity) -> Unit)? = null
) :
    RecyclerAdapter<WalletWithDrawEntity, ItemCoinExchangeBinding>(context),
    ItemTouchCallback.Callback {


    override fun getViewBinding(parent: ViewGroup): ItemCoinExchangeBinding {
        return ItemCoinExchangeBinding.inflate(inflater, parent, false)
    }

    override fun registerListener(holder: ItemViewHolder, binding: ItemCoinExchangeBinding) {
        binding.rflContent.setOnClickListener {
            getItem(holder.layoutPosition)?.let {
                onChanged!!.invoke(it)
            }

        }
    }

    override fun convert(
        holder: ItemViewHolder,
        binding: ItemCoinExchangeBinding,
        item: WalletWithDrawEntity,
        payloads: MutableList<Any>
    ) {
        binding.run {
            tvContent.text = "${(item.amount ?: "0").toDouble().finialDouble()}元"
            rtvTag.text = "阅读${item.time}分钟"
            tvDesc.text =
                "约${
                    if (TextUtils.isEmpty(item.amount)) "0" else {
                        val goldBalance = item.amount!!.toDouble()
                        val ratio = subRatio!!.toDouble()
                        (goldBalance * ratio).finialDouble()
                    }
                }金币"
        }
    }


}